package com.doudou.oauth.controller;

import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: 傻男人
 * @Date: 2020/5/19 9:06
 * @Version: 1.0
 * @Description: 自定义授权页面  默认请求地址为：/oauth/confirm_access
 * 如果授权页面请求地址随意定义 需要到继承AuthorizationServerConfigurerAdapter类中的configure(AuthorizationServerEndpointsConfigurer endpoints)方法添加：
 *  endpoints.pathMapping("/oauth/confirm_access","/custom/confirm_access");
 */
@Controller
@SessionAttributes("authorizationRequest")
public class GrantController {

    @RequestMapping("/oauth/confirm_access")
    public String getAccessConfirmation(Map<String, Object> model, HttpServletRequest request) throws Exception {
        Map<String, String> scopes = (Map<String, String>) (model.containsKey("scopes") ? model.get("scopes") : request.getAttribute("scopes"));
        List<String> scopeList = new ArrayList<>();
        if (scopes != null) {
            scopeList.addAll(scopes.keySet());
        }else {
            AuthorizationRequest authorizationRequest = (AuthorizationRequest)model.get("authorizationRequest");
            Set<String> scope = authorizationRequest.getScope();
            scopeList.addAll(scope);
        }
        model.put("scopeList", scopeList);
        return "grant.html";
    }

}

